package com.xiaoxiang.gateway.config;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.web.reactive.function.client.WebClient;

import javax.annotation.Resource;
import java.lang.reflect.Proxy;

/**
 * @author: haha
 * @date: 2022/3/31 19:07
 */
public class WebFluxFeignProxyFactoryBean<T> implements FactoryBean {


    private final Class<T> feignInterface;

    public WebFluxFeignProxyFactoryBean(Class<T> feignInterface) {
        this.feignInterface = feignInterface;
    }

    @Resource
    private WebClient webClient;

    @Override
    public Object getObject() throws Exception {
        WebFluxFeignProxy<T> feignProxy = new WebFluxFeignProxy<>(webClient, feignInterface);
        return Proxy.newProxyInstance(feignInterface.getClassLoader(),
                new Class[] {feignInterface}, feignProxy);
    }

    @Override
    public Class<?> getObjectType() {
        return feignInterface;
    }

}
